package com.xingdata.jjxc.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.text.Editable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.xingdata.jjxc.R;

public class Widget {

	@SuppressWarnings("unused")
	private final static String FILE_SAVEPATH = Environment
			.getExternalStorageDirectory().getAbsolutePath() + "/zzd/portrait/";

	// 处理ListView与ScrollView的显示问题
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		// 获取ListView对应的Adapter
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0); // 计算子项View 的宽高
			totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		// listView.getDividerHeight()获取子项间分隔符占用的高度
		// params.height最后得到整个ListView完整显示需要的高度
		listView.setLayoutParams(params);
	}

	// 处理ExpandableListView与scrollView的显示问题
	public static void setListViewHeightBasedOnChildren(
			ExpandableListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	// 获得屏幕高度
	public static int getScreenHeight(Context context) {
		DisplayMetrics displayMetrics = context.getResources()
				.getDisplayMetrics();
		return displayMetrics.heightPixels;
	}

	public static int getScreenWidth(Context context) {
		DisplayMetrics displayMetrics = context.getResources()
				.getDisplayMetrics();
		return displayMetrics.widthPixels;
	}

	public static void withDrawKeyboard(Activity avty) {
		InputMethodManager inputManger = (InputMethodManager) avty
				.getBaseContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputManger != null) {
			inputManger.hideSoftInputFromWindow(avty.getWindow().getDecorView()
					.getWindowToken(), 0);
		}
	}

	public static boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager connManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netInfo = connManager.getActiveNetworkInfo();
			if (netInfo != null) {
				return netInfo.isAvailable();
			}
		}
		return false;
	}

	public static void inputFocus(EditText edit) {
		Editable eb = edit.getText();
		edit.setSelection(eb.length());
	}

	// 【Activity跳转动画】
	public static void startActivityAnim(Activity avty, int in, int out) {
		@SuppressWarnings("deprecation")
		int VERSION = Integer.parseInt(android.os.Build.VERSION.SDK);
		if (VERSION >= 5) {
			avty.overridePendingTransition(in, out);
		}
	}

	public static void setNavigationBar(RelativeLayout lyt, ImageView iv,
			TextView tv, boolean type) {
		if (type == true) {
			lyt.setSelected(true);
			iv.setSelected(true);
			tv.setSelected(true);
		} else {
			lyt.setSelected(false);
			iv.setSelected(false);
			tv.setSelected(false);
		}
	}

	public static void checkVersion(final Context context, String code,
			final String softUrl) {
			new AlertDialog.Builder(context)
					.setTitle("驾驾行车有新版本上线，是否更新？")
					.setNegativeButton("退出",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									((Activity)context).finish();
								}
							})
					.setPositiveButton("立即更新",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									((Activity)context).startActivity(new Intent(
											Intent.ACTION_VIEW,
											// Uri.parse(GetAppCxt.ZZD_DOWNLOAD)));
											Uri.parse(softUrl)));
									dialog.dismiss();
								}
							}).show();
	}

	/**
	 * input输入框获取焦点和失去焦点不容的背景状态
	 * 
	 * @param context
	 *            上下文
	 * @param et
	 *            edittext
	 * @param iv
	 *            要设置背景imageView
	 */
	public static void EditTextFoucListener(final Context context, View et,
			final ImageView iv) {
		et.setOnFocusChangeListener(new OnFocusChangeListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					iv.setBackgroundDrawable(context.getResources()
							.getDrawable(R.drawable.zzd_et_onfocus));
				} else {
					iv.setBackgroundDrawable(context.getResources()
							.getDrawable(R.drawable.zzd_et_offfocus));
				}
			}
		});
	}

	public static void setLayoutAnim(Context context, Animation anim,
			RelativeLayout layout, int res, int duration) {
		anim = AnimationUtils.loadAnimation(context, res);
		anim.setDuration(duration);
		layout.startAnimation(anim);
	}
}
